#include "FileAppender.h"
#include <fstream>
#ifndef WIN32
#include <syslog.h>
#endif

namespace catan
{
	FileAppender::FileAppender(void)
	{
#ifdef _WIN32
	#ifndef AVATARBOX_BUILD
		mIsCreateNewFile = true;
	#else
		mIsCreateNewFile = false;
	#endif
#else
		mIsCreateNewFile = false;
#endif	

	}

	FileAppender::FileAppender( const tstring& fileName )
	{
		new(this)FileAppender(); 
		mFileName = fileName;
		if(mIsCreateNewFile)
		{
			tofstream outFile(mFileName.c_str(), std::ios::trunc | std::ios::out);
		}
	}

	FileAppender::~FileAppender(void)
	{
	}

	void FileAppender::Output( const tstring& outstring )
	{
		tofstream outFile(mFileName.c_str(), std::ios::app | std::ios::out);
#ifndef WIN32
		static bool loged = false;
		if (!loged && !outFile.is_open())
		{
			openlog("SimpleLog", LOG_CONS | LOG_PID, 0);
			syslog(LOG_ERR, "Cant not Log file at %s\n", mFileName.c_str());
			closelog();
			loged = true;
		}
#endif
		outFile << outstring;
		outFile.flush();
	}
}

